import 'package:cat_eye/bean/home_body_bean.dart';
import 'package:cat_eye/bean/sq_lite_bean.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/sqflite/sqLite_manager.dart';
import 'package:cat_eye/util/date_util.dart';
import 'package:cat_eye/util/util.dart';
import 'package:flutter/material.dart';

/*
* @Package: root_page.home_page
* @ClassName: hom_video_page_widget
* @Author: szj
* @CreateDate: 7/30/21 2:51 PM
*
* 首页底部视频页面
*/
// ignore: must_be_immutable
class HomeVideoPageWidget extends StatelessWidget {
  ItemList video;

  HomeVideoPageWidget({Key key, @required this.video}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //顶部Image
        _headImage(context),
        //底部布局

        Row(
          children: [
            //左侧作者图片
            _leftImage(),
            //中间文字
            _videoDescription(),
            //右侧分享
            _shareButton(),
          ],
        )
      ],
    );
  }

  /// 分享按钮
  Widget _shareButton() {
    return IconButton(
      icon: Icon(Icons.share, color: Colors.black38),
      onPressed: () => share(video.data.title, video.data.playUrl),
    );
  }

  /// 视频内容简介
  Widget _videoDescription() {
    // Expanded:相当于Android中设置 weight 权重
    return Expanded(
      flex: 1,
      child: Container(
        padding: EdgeInsets.only(left: 10),
        // 垂直的LinearLayout
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(video.data.title,
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                    fontWeight: FontWeight.bold),
                // 过长则省略
                overflow: TextOverflow.ellipsis),
            Padding(
                padding: EdgeInsets.only(top: 2),
                child: Text(
                    video.data.author == null
                        ? video.data.description
                        : video.data.author.name,
                    style: TextStyle(color: Color(0xff9a9a9a), fontSize: 12)))
          ],
        ),
      ),
    );
  }

  Widget _leftImage() {
    return Padding(
      padding: EdgeInsets.only(left: 10, top: 5),
      child: ClipRRect(
          borderRadius: BorderRadius.circular(22),
          child: cacheImage(
            video.data.author == null
                ? video.data.provider.icon
                : video.data.author.icon,
            width: 40,
            height: 40,
          )),
    );
  }

  Widget _headImage(BuildContext context) {
    return Container(
      height: 180,
      alignment: Alignment.center,
      padding: EdgeInsets.only(top: 10, left: 10, right: 10),
      width: MediaQuery.of(context).size.width + 10,
      child: Stack(
        children: [
          Container(
            // decoration:装饰,设置子控件的背景颜色、形状等
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12),
              image: DecorationImage(
                // 网络获取图片 只有这样才能设置圆角
                image: cachedNetworkImageProvider(video.data.cover.feed),
                // 图片显示样式,类似Android缩放设置
                fit: BoxFit.cover,
              ),
            ),

            //这样不能设置圆角
            // child: cacheImage(bannerList[index].data.cover.feed,fit: BoxFit.cover),
          ),

          //左上角文字
          leftTopText(video.data.category),

          //右下角时间
          leftBottomTime(formatDateMsByMS(video.data.duration * 1000)),

          //右上角关注按钮
          rightTopFollow(context),
        ],
      ),
    );
  }

  Widget rightTopFollow(BuildContext context) {
    return Positioned(
      right: 10,
      top: 10,
      child: InkWell(
        onTap: () => sqLiteInstance(context),
        child: Container(
          padding: EdgeInsets.only(left: 5, right: 5, top: 3, bottom: 3),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(5)),
          child: Text(
            StringConfig.FOLLOW,
            style:
                TextStyle(color: Colors.black26, backgroundColor: Colors.white),
          ),
        ),
      ),
    );
  }

  //添加数据
  void sqLiteInstance(BuildContext context) {
    SqLiteBean bean = SqLiteBean(
      video.data.category,
      video.data.cover.feed,
      video.data.author == null
          ? video.data.provider.icon
          : video.data.author.icon,
      video.data.title,
      video.data.author == null
          ? video.data.description
          : video.data.author.name,
      formatDateMsByMS(video.data.duration * 1000),
      video.data.playUrl,
      '${video.data.cover.blurred}',
    );

    SQLiteManager.getInstance().insert(bean, context);
  }

  Widget leftTopText(String category) {
    return Positioned(
      left: 10,
      top: 10,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white54,
          borderRadius: BorderRadius.all(Radius.circular(22)),
        ),
        width: 44,
        height: 44,
        alignment: Alignment.center,
        child: Text(video.data.category ?? StringConfig.OTHER),
      ),
    );
  }

  Widget leftBottomTime(String time) {
    return Positioned(
        right: 10,
        bottom: 10,
        child: Container(
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10), color: Colors.black54),
          width: 50,
          height: 30,
          alignment: Alignment.center,
          child: Text(
            time,
            style: TextStyle(color: Colors.white),
          ),
        ));
  }
}
